<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class StorerestController extends Uuch_RestController {
    public function indexAction() {
        
    }
    
    public function getAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            return;
        }
        if(!method_exists($this,$params["functionName"])){
            return array("Result"=>1006,"ResultMessage"=>"所需要的服务不存在或者暂不可用");
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function postAction() {
        return false;
    }
    
    public function putAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            exit();
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params["params"]);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function deleteAction() {
        return false;
    }
    
    /**
     * @return 获取个人云仓库详情
     */
    protected function StoreInfo($params) {
        $isAuth = $this->analysisAuth($params["head"]);
        if($isAuth["Result"] != 1) {
            return $isAuth;
        }
        $storeItemSearch = new Application_Model_Filter_StoreItemSearch();
        $storeItemSearch->userId = $isAuth["userId"];
        $storeItemSearch->state = Application_Model_StoreItem::STATUS_InStored;
        $pageindex = $params["idx"]?($params["idx"]==1?0:($params["idx"]-1)*10):0;
        $storeItem = Application_Model_StoreItem::getMyByFilter($storeItemSearch, 10, $pageindex);
        $returnData = array();
        if($storeItem){
            $returnData = $this->analysisStore($storeItem);
        }
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "获取成功";
        $returnData["auth"] = Uuch_Util::encryptString($isAuth["userId"]."&".date("Y-m-d H:i:s"));
        return $returnData;
    }
    /**
     * @return 解析返回仓库详情所需数据
     */
    protected function analysisStore($storeItem) {
        $returnData = array();
        $storeNum = 0;
        $storeWeight = 0;
        $i = 0;
        foreach ($storeItem["list"] as $storeVal) {
            $returnData["storeInfo"][$i]["storeId"] = $storeVal->Id;
            $returnData["storeInfo"][$i]["storeName"] = $storeVal->ProductName;
            $returnData["storeInfo"][$i]["storeLogisticsName"] = $storeVal->LogisticsName;
            $returnData["storeInfo"][$i]["storeLogistics"] = $storeVal->Logistics;
            $returnData["storeInfo"][$i]["storeImg"] = $this->cdn.is_file('big'.$storeVal->imageFile)?('/big'.$storeVal->imageFile):($storeVal->imageFile);
            $returnData["storeInfo"][$i]["storeWeight"] = round($storeVal->weight);
            $storeNum ++;
            $storeWeight += round($storeVal->weight);
            $i ++;
        }
        $returnData["count"] = $storeItem["size"];
        $returnData["pages"] = $storeItem["pages"];
        $returnData["storeNum"] = $storeNum;
        $returnData["storeSumWeight"] = $storeWeight;
        return $returnData;
    }
}
